<template>
	<view>
		<u-navbar :title="$t(title)"></u-navbar>
		<view class="goodsList">
			<!-- <checkbox-group> -->
			<view class="list">
				<view class="goods">
					<image :src="goodsinfo.goods_data.image" mode="" ></image>
					<view class="info">
						<view class="tit" >{{goodsinfo.goods_data.title}}</view>
						<view class="bi" v-if="goodsinfo.status == 0" >
							{{ $t("一共可转换") }}：<text>{{exchange_integral*goods_num}}</text>{{ $t("糯米值") }}
						</view>
						<view class="num">
							<u-number-box :min="1" v-model="goods_num" :max="xz_count"></u-number-box>
						</view>
					</view>
				</view>
			</view>		
			<view class="" style="height: 100rpx;"></view>
			<!-- </checkbox-group> -->
			<u-empty text="暂无数据" mode="list" :show="nullShow" margin-top="400"></u-empty>
		</view>
		<view class="bottom11 n-flex-row n-justify-between n-align-center">			
			<view class="yixuan" v-if="type==1">
				<view class="text">
					{{ $t("已选") }}{{goods_num}}{{ $t("件") }}|{{ $t("合计") }}：<view>{{xz_jiazhi}}<text>{{ $t("糯米值") }}</text></view>
				</view>
			</view>
			<view class="yixuan" v-if="type==3">
				<view class="text">
					{{ $t("已选") }}{{goods_num}}{{ $t("件") }}
				</view>
			</view>
			<view class="btns n-flex-row" v-if="type==1">
				<view class="btn ti" @click="duihuan1"> {{ $t("立即转换") }} </view>
			</view>
			<view class="btns n-flex-row" v-if="type==2">
				<view class="btn ti" @click="zhuanzheng">  {{ $t("立即转增") }}</view>
			</view>
			<view class="btns n-flex-row" v-if="type==3">
				<view class="btn ti" @click="tihuo"> {{ $t("立即提货") }} </view>
			</view>
		</view>
		<u-popup v-model="zhuan" mode="center" border-radius="20">
			<view class="zhuan">
				<view class="tit">{{ $t("请输入转赠人的手机号") }}</view>
				<u-input v-model="phone" type="number" maxlength="11" :border="true" placeholder="请输入转赠人注册手机号" />
				<view class="tip">{{ $t("注：一旦转赠成功不可退货，请谨慎操作") }}</view>
				<view class="btns1">
					<view class="btn11 a" @click="zhuan = false">{{ $t("取消") }}</view>
					<view class="btn11 b" @click="zhuanz">{{ $t("确定") }}</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title:'',
				type:0,
				nullShow:false,
				isOne: '0',
				xz_count:0,
				xz_jiazhi:0,
				zhuan:false,
				phone: '', //转赠人手机号
				zhuanId: '',
				id:0,
				goods_id:0,
				goods_num:0,
				exchange_integral:0,
				freight_rule_text:'',
				goodsinfo:{},
			}
		},
		onLoad(option) {
			this.type = option.type;
			this.goods_id = option.goods_id;
			if(this.type==1){
				this.title = '批量转换';
			}
			if(this.type==2){
				this.title = '批量转增';
			}
			if(this.type==3){
				this.title = '批量提货';
			}
		},
		onShow() {
			this.getData(0);
		},
		onUnload() {
			// uni.$off('huishou_reload')
		},
		//计算属性
		methods: {
			//全选
			//单选
			duihuan1() {
				let arr = []
				this.goodsList.forEach(item => {
					if (item.check == true) {
						arr.push(item)
					}
				})
				if(arr.length == 0){
					this.$tip.tip("请选择商品")
					return
				}
				let data = []
				arr.forEach(val => {
					data.push({
						image: val.goods_data.image,
						name: val.goods_data.title,
						price: val.exchange_integral,
						id:val.id
					})
				})
				uni.setStorageSync('dui', data)
				uni.navigateTo({
					url: '/pages/cangku/duihuan'
				})
			},
			zhuanzheng(){
				let arr = []
				this.goodsList.forEach(item => {
					if (item.check == true) {
						arr.push(item.id)
					}
				})
				if(arr.length == 1){
					arr = arr[0]
				}else{
					arr = arr.join()
				}
				console.log(arr);
				this.zhuanId = arr
				if(arr.length == 0){
					this.$tip.tip("请选择商品")
					return
				}
				this.zhuan = true;
			},
			// 确定转赠
			zhuanz() {
				if (!this.$u.test.mobile(this.phone)) {
					this.$tip.tip("请输入正确的手机号")
					return
				}
				this.$http({
					url: "api/order/prize_give",
					data: {
						id: this.zhuanId,
						receive_username: this.phone
					}
				}).then(res => {
					console.log(res);
					if (res.data.code == 1) {
						this.zhuan = false
						this.phone = ""
						this.current = 2
						this.swiperCurrent = 2
						this.$tip.tip(res.data.msg)
						setTimeout(() => {
							uni.navigateBack({});
						}, 3000)
					} else {
						this.$tip.tip(res.data.msg)
					}
				}).catch(err => {});
			},
			//提货
			tihuo(){
				if(this.goods_num <= 0){
					this.$tip.tip("请选择提货商品")
					return
				}
				uni.navigateTo({
					url: '/pagesB/pages/cangku/confirmOrder_th?goods_id='+this.goods_id+'&goods_num='+this.goods_num
				})
			},
			getData(sta) {
				uni.showLoading({
					title: "加载中"
				})
				this.goodsinfo = {};
				this.$http({
					url: "api/order/get_prize_record_goods",
					data: {
						status: sta,
						goods_id: this.goods_id,
					}
				}).then(res => {
					uni.hideLoading()
					if (res.data.code == 1) {
						this.goodsinfo = res.data.data.row;
						this.exchange_integral = res.data.data.row.exchange_integral*1;
						this.xz_count = res.data.data.row.number;
						this.goods_num = res.data.data.row.number;
					} else {
						this.$tip.tip(res.data.msg)
					}
				}).catch(err => {});
			},
		}
	}
</script>

<style>
	@import url("../../../static/css/piliang_edit.css");
</style>
